/**
 *  @file EducationalResources.java
 *  @author Mamontov
 *
 *  This file contains EducationalResources class definition
 */
package db21.entities;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.rdf.model.Property;
import db21.racerpro.Queries;
import java.util.ArrayList;

/**
 * @class  EducationalResources
 * @author Mamontov
 *
 *
 */
public class EducationalResources extends Entities {
    
    public EducationalResources() {
        m_conceptname = "EducationalResource";
    }
    /**
     * You should use only this function to create new resource, because only
     * it cares about necessary assertion
     * @param resource
     * @return whether it was successfully created
     */
    public boolean add(EducationalResource resource) {
        Individual ind = this.makeAndReturnIndividual(resource.typeName, resource.name);
        if (ind == null) return false;
        // Assert type of resource
        String propName;
        if (resource.typeName.equals("DigitalEducationalResource")) {
            propName = Queries.getDigitalEducationalResourceTypePropertyName();           
        } else {
            propName = Queries.getCommonEducationalResourceTypePropertyName();
        }
        ind.addProperty( m_conn.getProperty(propName),  m_conn.getIndividual(resource.typeValue));
        // Assert themes
        Property prop = m_conn.getProperty(Queries.includeThemes());
        for(int i = 0; i < resource.themes.size(); i++) {
            Individual theme = m_conn.getIndividual(resource.themes.get(i));
            ind.addProperty(prop, theme);
        }
        // Assert competences
        prop = m_conn.getProperty(Queries.createCompetences());
        for(int i = 0; i < resource.competences.size(); i++) {
            Individual competence = m_conn.getIndividual(resource.competences.get(i));
            ind.addProperty(prop, competence);
        }
        m_conn.syncWithRacerPro();
        return true;
    }
    
    public EducationalResource fetch(String iri) {
        String escaped = Queries.escapeValue(m_conn.prefix(), iri);
        EducationalResource result = new EducationalResource();
        result.name = iri;
        String q;
        
        result.typeName = m_conn.getClassOfIndividual(iri);
        ArrayList<String> list;
        if (result.typeName.equals("DigitalEducationalResource")) {
            q = m_conn.buildQuery(Queries.getPropertyValues(), escaped, Queries.getDigitalEducationalResourceTypePropertyName());
        } else {
            q = m_conn.buildQuery(Queries.getPropertyValues(), escaped, Queries.getCommonEducationalResourceTypePropertyName());
        }
        list = this.fetchAsRow(q);
        // Sometimes RacerPro just don't feel like sending some actual data to us
        // so it returns zero length query.        
        if (!list.isEmpty()) {
            result.typeValue = list.get(0);
        } else {
            result.typeValue = "";
        }
        
        
        q = m_conn.buildQuery(Queries.getPropertyValues(), escaped, Queries.includeThemes());
        result.themes = this.fetchAsRow(q);
        
        q = m_conn.buildQuery(Queries.getPropertyValues(), escaped, Queries.createCompetences());
        result.competences = this.fetchAsRow(q);
        
        
        return result;
    }
    
    public ArrayList<String> listForTheme(String iri) {
        String escaped = Queries.escapeValue(m_conn.prefix(), iri);
        String query  = m_conn.buildQuery(Queries.resourceByTheme(), escaped);
        return this.fetchAsRow(query);
    }
    
    public ArrayList<String> listForSubject(String iri) {
        String escaped = Queries.escapeValue(m_conn.prefix(), iri);
        String query  = m_conn.buildQuery(Queries.resourceBySubject(), escaped);
        return this.fetchAsRow(query);
    }
    
    public ArrayList<String> listForCompetence(String iri) {
        String escaped = Queries.escapeValue(m_conn.prefix(), iri);
        String query  = m_conn.buildQuery(Queries.resourceByCompetence(), escaped);
        return this.fetchAsRow(query);
    }
    
}
